package org.example.BinaryTree;

import java.util.ArrayDeque;

public class Flip_Binary_Tree {
    public static void main(String[] args) {
        //翻转二叉树

        //给你一棵二叉树的根节点 root ，翻转这棵二叉树，并返回其根节点。
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode root = new TreeNode(0);
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);
        root.left = node1;
        root.right = node2;
        node1.left = node3;
        node1.right = node4;
        node2.left = node5;
        node2.right = node6;
        node5.left = node7;
        get(root);
        //遍历
        search(root);
    }
    public static TreeNode get(TreeNode root) {
        if (root==null) return root;
        ArrayDeque<TreeNode> deque = new ArrayDeque<>();
        deque.add(root);
        while(!deque.isEmpty()){
            int len = deque.size();
            while(len>0){
                TreeNode node = deque.poll();
                TreeNode temp = node.left;
                node.left = node.right;
                node.right = temp;
                if (node.left!=null) deque.add(node.left);
                if (node.right!=null) deque.add(node.right);
                len--;
            }
        }
        return root;
    }
    public static void search(TreeNode root){
        if (root==null) return;
        System.out.println(root.val);
        search(root.left);
        search(root.right);
    }
}
